/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.unipar.oficina.service.cadastros;

import br.com.unipar.oficina.modelo.cadastros.Cargo;
import br.com.unipar.oficina.modelo.cadastros.Colaborador;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

/**
 *
 * @author Thiago Marques
 */
@Service("colaboradorService")
public class ColaboradorServiceImpl
        extends GenericServiceImpl<Colaborador>
        implements ColaboradorService {

    
    @Override
    public List<Colaborador> consultar(String matricula, String nome) {
        Criteria crit = ((Session) entityManager.getDelegate()).createCriteria(Colaborador.class);
        if (matricula != null) {
            crit.add(Restrictions.ilike("matricula", matricula, MatchMode.ANYWHERE));
        }
        if (nome != null) {
            crit.add(Restrictions.ilike("nome", nome, MatchMode.ANYWHERE));
        }
//        if (nomeCargo != null) {
//            crit.createAlias("cargo", "cargo");
//            crit.add(Restrictions.ilike("cargo.nome", nomeCargo, MatchMode.ANYWHERE));
//        }
        return crit.list();
    }

    @Override
    public Colaborador getByIdColaborador(Long id) {
        return entityManager.find(Colaborador.class, id);
    }

    @Override
    public Cargo getByIdCargo(Long id) {
        return entityManager.find(Cargo.class, id);
    }

    @Override
    public List<Cargo> getCargos() {
        Criteria crit = ((Session) entityManager.getDelegate()).createCriteria(Cargo.class);
        return crit.list();
    }

    @Override
    public List<Colaborador> getColaboradores(Cargo nomeCargo) {
        Criteria crit = ((Session) entityManager.getDelegate()).createCriteria(Colaborador.class);
        crit.add(Restrictions.eq("cargo", nomeCargo));
        return crit.list();
    }
}
